﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Forms.Design;

using DevExpress.XtraEditors;
using DevExpress.XtraGrid;

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesRaster;

namespace SZ
{
    public partial class AddDataDialogFrm : DevExpress.XtraEditors.XtraForm
    {
        #region 变量
        
        string desktopString = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//获取桌面的路径
        private AxMapControl m_mapControl = null;

        #endregion

        public AddDataDialogFrm(AxMapControl mapControl)
        {
            InitializeComponent();
            IntiGridControl();
            m_mapControl = mapControl;
        }

        public AddDataDialogFrm()
        {
            InitializeComponent();
            IntiGridControl();
            for (int i = 0; i < 10; i++)
            {
                PublicVariablesClass.AddRasterFilePath[i] = null ;
            }
 
        }

        public void IntiGridControl()
        {
            #region 设置字段名
            //图片列
            DevExpress.XtraGrid.Columns.GridColumn colPic = new DevExpress.XtraGrid.Columns.GridColumn();
            colPic.Name = "colPic";
            colPic.Caption = "";
            colPic.VisibleIndex = 0;
            colPic.FieldName = "colPic";
            colPic.OptionsColumn.ShowCaption = true;
            colPic.OptionsColumn.FixedWidth = true;
            colPic.Width = 15;

            //名称列
            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
            colName.Name = "colName";
            colName.Caption = " 名称";
            colName.FieldName = "colName";
            colName.VisibleIndex = 1;
            colName.Width = 200;

            //类型列
            DevExpress.XtraGrid.Columns.GridColumn colType = new DevExpress.XtraGrid.Columns.GridColumn();
            colType.Name = "colType";
            colType.Caption = "类型";
            colType.FieldName = "colType";
            colType.VisibleIndex = 2;
            colType.Width = 60;

            //修改时间列
            DevExpress.XtraGrid.Columns.GridColumn colTime = new DevExpress.XtraGrid.Columns.GridColumn();
            colTime.Name = "colTime";
            colTime.Caption = "修改时间";
            colTime.FieldName = "colTime";
            colTime.VisibleIndex = 3;
            colTime.Width = 200;

            //图片字段的设置
            DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit reposityItemPic = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
            reposityItemPic.AutoHeight = true;
            reposityItemPic.Name = "reposityItemPic1";
            colPic.ColumnEdit = reposityItemPic;  //不是很理解

            this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colPic, colTime, colType, colName });
            this.gridView1.BestFitColumns();
            #endregion

            #region 加载磁盘名
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.DriveType == DriveType.Fixed)
                {
                    this.directoryComboBoxEdit1.Properties.Items.Add(drive.Name);
                }
            }
            if (!this.directoryComboBoxEdit1.Properties.Items.Contains(desktopString)) //判断是否包含桌面的路径
            {
                this.directoryComboBoxEdit1.Properties.Items.Add(desktopString);
            }
            this.directoryComboBoxEdit1.Text = this.directoryComboBoxEdit1.Properties.Items[0].ToString();
            #endregion

            ChangeView();
        }

        //核心函数，当目录combo中的路径发生改变时，gridView中的视图也发生改变
        public void ChangeView()
        {
            DataTable m_dataTable = new DataTable();
            m_dataTable.Columns.Add("colPic", typeof(Image));
            m_dataTable.Columns.Add("colName", typeof(string));
            m_dataTable.Columns.Add("colType", typeof(string));
            m_dataTable.Columns.Add("colTime", typeof(string));
            try
            {
                DataRow m_dataRow;

                //获取磁盘内的目录
                DirectoryInfo directoryInfo = new DirectoryInfo(this.directoryComboBoxEdit1.Text.Trim());
                if (directoryInfo == null)
                {
                    return;
                }
                foreach (DirectoryInfo dir in directoryInfo.GetDirectories())
                {
                    m_dataRow = m_dataTable.Rows.Add();
                    if (System.IO.File.Exists(dir.FullName))
                    {
                    }
                    else
                    {
                        m_dataRow["colPic"] = this.imageList1.Images[0];
                        m_dataRow["colName"] = dir.Name;
                        m_dataRow["colType"] = "文件夹";
                        m_dataRow["colTime"] = dir.CreationTime.ToLocalTime().ToString();
                    }
                }
                //遍历文件列表
                foreach (FileInfo file in directoryInfo.GetFiles())
                {
                    int index = file.Name.LastIndexOf(".");
                    //MessageBox.Show(file.Name);
                    //MessageBox.Show(index.ToString());
                    string strType = file.Name.Substring(index + 1);

                    if (strType.ToUpper().Contains("SHP"))
                    {
                        #region shapfile数据
                        m_dataRow = m_dataTable.Rows.Add();
                        m_dataRow["colName"] = file.Name;

                        IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                        IFeatureWorkspace pFeatureWorkspace;
                        pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(this.directoryComboBoxEdit1.Text, 0) as IFeatureWorkspace;
                        IFeatureClass pFeautreClass;
                        pFeautreClass = pFeatureWorkspace.OpenFeatureClass(file.Name);

                        //判断要素是注记还是几何要素
                        if (pFeautreClass.FeatureType == esriFeatureType.esriFTAnnotation)
                        {
                            m_dataRow["colPic"] = this.imageList1.Images[8];
                            m_dataRow["colType"] = "图层注记";
                        }
                        else
                        {
                            if (pFeautreClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                            {
                                m_dataRow["colPic"] = this.imageList1.Images[5];
                                m_dataRow["colType"] = "点状要素";
                            }
                            else if (pFeautreClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                            {
                                m_dataRow["colPic"] = this.imageList1.Images[6];
                                m_dataRow["colType"] = "线状要素";
                            }
                            else if (pFeautreClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                            {
                                m_dataRow["colPic"] = this.imageList1.Images[7];
                                m_dataRow["colType"] = "面状要素";
                            }
                        }
                        m_dataRow["colTime"] = file.CreationTime.ToLocalTime().ToString();

                        //回收工作空间
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureWorkspace);
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        #endregion
                    }

                    else if (strType.ToUpper().Contains("MDB"))
                    {


                        m_dataRow = m_dataTable.Rows.Add();
                        m_dataRow["colPic"] = this.imageList1.Images[1];
                        m_dataRow["colName"] = file.Name;
                        m_dataRow["colType"] = "MDB数据";
                        m_dataRow["colTime"] = file.CreationTime.ToLocalTime().ToString();
                    }

                    else if (strType.ToUpper().Contains("JPG") || strType.ToUpper().Contains("TIF") || strType.ToUpper().Contains("BMP"))
                    {
                        #region 栅格数据
                        m_dataRow = m_dataTable.Rows.Add();
                        m_dataRow["colPic"] = this.imageList1.Images[2];
                        m_dataRow["colName"] = file.Name;
                        m_dataRow["colType"] = "栅格数据";
                        m_dataRow["colTime"] = file.CreationTime.ToLocalTime().ToString();
                        #endregion
                    }
                }
                this.gridView1.GridControl.DataSource = m_dataTable;
            }
            catch
            {

            }

        }

        private void directoryComboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeView();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            try
            {
                int count;//选取的要素数
                count = this.gridView1.SelectedRowsCount;
                if (count == 1)
                {
                    DataRow m_dataRow = this.gridView1.GetFocusedDataRow();
                    string type = m_dataRow["colType"].ToString();
                    fileNameTextEdit.Text = m_dataRow["colName"].ToString();
                    fileTypeComboBoxEdit.Text = "数据类型（" + type + ")";
                }
                else
                {
                    this.fileNameTextEdit.Text = "";
                    DataRow dr;
                    int[] dataRowHandles = this.gridView1.GetSelectedRows();
                    for (int i = 0; i < count; i++)
                    {
                        dr = this.gridView1.GetDataRow(dataRowHandles[i]);
                        this.fileNameTextEdit.Text = this.fileNameTextEdit.Text + dr["colName"] + ";";
                    }
                    this.fileTypeComboBoxEdit.Text = "数据类型";


                }
            }
            catch
            {
            }
        }

        private void toLastViewBtn_Click(object sender, EventArgs e)
        {
            int count = 0;
            int i;
            string strPath = this.directoryComboBoxEdit1.Text.Trim();
            i = strPath.IndexOf("\\"); //“\\”在路径字符串中首次出现的位置

            while (i >= 0)
            {
                count++;
                i = strPath.IndexOf("\\", i + 1);
            }

            //如果目录索引为根目录，则返回
            if (count < 2)
            {
                return;
            }


            if (count >= 3)
            {
                #region mdb中的数据集中的要素集中的内容，最少为三级目录

                int index1 = this.directoryComboBoxEdit1.Text.Trim().LastIndexOf("\\");
                string str1 = this.directoryComboBoxEdit1.Text.Trim().Remove(index1);
                int index2 = str1.LastIndexOf("\\");
                //mdb路径
                string str2 = str1.Remove(index2);
                int index3 = str2.Length;//mdb路径的长度，用来判断最后三个字符是否为mdb
                string str3 = str2.Substring(index3 - 3);


                try
                {
                    if (str3.ToUpper() == "MDB")
                    {
                        #region  当是mdb数据集中的要素类时

                        DataTable m_dataTable = new DataTable();
                        m_dataTable.Columns.Add("colPic", typeof(Image));
                        m_dataTable.Columns.Add("colName", typeof(string));
                        m_dataTable.Columns.Add("colType", typeof(string));
                        m_dataTable.Columns.Add("colTime", typeof(string));
                        DataRow m_dataRow;


                        IWorkspaceFactory pWorkspaceFactory;
                        IWorkspace pWorkspace;
                        pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                        pWorkspace = pWorkspaceFactory.OpenFromFile(str2, 0);

                        IEnumDataset pEnumDataset;
                        pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny);
                        pEnumDataset.Reset();

                        IDataset pDataset;
                        pDataset = pEnumDataset.Next();
                        while (pDataset != null)
                        {
                            m_dataRow = m_dataTable.Rows.Add();
                            #region 遍历数据集
                            m_dataRow["colName"] = pDataset.Name;
                            if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                            {
                                #region 要素类
                                IFeatureClass pFeatureClass = pDataset as IFeatureClass;

                                if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                                {
                                    m_dataRow = m_dataTable.Rows.Add();
                                    m_dataRow["colPic"] = this.imageList1.Images[8];
                                    m_dataRow["colType"] = "图层注记";
                                }
                                else
                                {
                                    if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                                    {
                                        m_dataRow = m_dataTable.Rows.Add();
                                        m_dataRow["colPic"] = this.imageList1.Images[5];
                                        m_dataRow["colType"] = "点状要素";
                                    }
                                    else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                                    {
                                        m_dataRow = m_dataTable.Rows.Add();
                                        m_dataRow["colPic"] = this.imageList1.Images[6];
                                        m_dataRow["colType"] = "线状要素";
                                    }
                                    else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                                    {
                                        m_dataRow = m_dataTable.Rows.Add();
                                        m_dataRow["colPic"] = this.imageList1.Images[7];
                                        m_dataRow["colType"] = "面状要素";
                                    }
                                }
                                //m_dataRow["colTime"] = pDataset.CreationTime.ToLocalTime().ToString();


                                #endregion
                            }
                            else if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                            {
                                #region 要素集
                                m_dataRow["colPic"] = this.imageList1.Images[3];
                                m_dataRow["colType"] = "要素集";
                                m_dataRow["colTime"] = "";
                                #endregion
                            }
                            else if (pDataset.Type == esriDatasetType.esriDTRasterDataset)
                            {
                                #region 栅格数据
                                m_dataRow["colPic"] = this.imageList1.Images[2];
                                m_dataRow["colType"] = "栅格数据";
                                m_dataRow["colTime"] = "";
                                #endregion
                            }
                            pDataset = pEnumDataset.Next();
                            #endregion
                        }
                        this.directoryComboBoxEdit1.Text = str2 + "\\";
                        this.gridView1.GridControl.DataSource = m_dataTable;
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumDataset);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspace);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspaceFactory);
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        #endregion

                    }
                    else
                    {
                        #region 当是普通文件时
                        string path = this.directoryComboBoxEdit1.Text.Trim().Remove(this.directoryComboBoxEdit1.Text.Trim().Length - 1);
                        path = path.Substring(0, path.LastIndexOf("\\") + 1);
                        this.directoryComboBoxEdit1.Text = path;
                        ChangeView();
                        #endregion
                    }
                }
                catch
                {
                }
                #endregion
            }
            else
            {
                #region "\\"数量为2的情况
                string path = this.directoryComboBoxEdit1.Text.Trim().Remove(this.directoryComboBoxEdit1.Text.Trim().Length - 1);
                path = path.Substring(0, path.LastIndexOf("\\") + 1);
                this.directoryComboBoxEdit1.Text = path;
                ChangeView();
                #endregion

            }
        }

        private void connectToFolderBtn_Click(object sender, EventArgs e)
        {
            FolderDialogClass folderDialog = new FolderDialogClass();

            if (folderDialog.DisplayDialog() == DialogResult.OK)
            {
                string path = folderDialog.Path.ToString();
                this.directoryComboBoxEdit1.Text = path;
                ChangeView();
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            DataTable m_dataTable = new DataTable();
            DataRow m_dataRow = this.gridView1.GetFocusedDataRow();//将点击的row设置为当前的row

            if (m_dataRow["colType"].ToString() == "图层注记" ||
                m_dataRow["colType"].ToString() == "点状要素" ||
                m_dataRow["colType"].ToString() == "线状要素" |
                m_dataRow["colType"].ToString() == "面状要素" ||
                m_dataRow["colType"].ToString() == "栅格数据")
            {
                return;
            }
            string strFileName = m_dataRow["colName"].ToString();
            this.directoryComboBoxEdit1.Text = this.directoryComboBoxEdit1.Text + strFileName + "\\";

            if (m_dataRow["colType"].ToString() == "文件夹")
            {
                ChangeView();
            }
            else if (m_dataRow["colType"].ToString() == "MDB数据")
            {
                #region Mdb数据

                m_dataTable.Columns.Add("colPic", typeof(Image));
                m_dataTable.Columns.Add("colName", typeof(string));
                m_dataTable.Columns.Add("colType", typeof(string));
                m_dataTable.Columns.Add("colTime", typeof(string));

                int index = this.directoryComboBoxEdit1.Text.LastIndexOf("\\");
                m_dataRow = m_dataTable.Rows.Add();

                IWorkspaceFactory pWorkspaceFactory;
                IWorkspace pWorkspace;
                pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                pWorkspace = pWorkspaceFactory.OpenFromFile(this.directoryComboBoxEdit1.Text.Remove(index), 0);

                IEnumDataset pEnumDataset;
                pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny);
                pEnumDataset.Reset();

                IDataset pDataset;
                pDataset = pEnumDataset.Next();
                while (pDataset != null)
                {
                    #region 遍历数据集
                    m_dataRow["colName"] = pDataset.Name;
                    if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                    {
                        #region 要素类
                        IFeatureClass pFeatureClass = pDataset as IFeatureClass;

                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                        {
                            m_dataRow = m_dataTable.Rows.Add();
                            m_dataRow["colPic"] = this.imageList1.Images[8];
                            m_dataRow["colType"] = "图层注记";
                        }
                        else
                        {
                            if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                            {
                                m_dataRow = m_dataTable.Rows.Add();
                                m_dataRow["colPic"] = this.imageList1.Images[5];
                                m_dataRow["colType"] = "点状要素";
                            }
                            else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                            {
                                m_dataRow = m_dataTable.Rows.Add();
                                m_dataRow["colPic"] = this.imageList1.Images[6];
                                m_dataRow["colType"] = "线状要素";
                            }
                            else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                            {
                                m_dataRow = m_dataTable.Rows.Add();
                                m_dataRow["colPic"] = this.imageList1.Images[7];
                                m_dataRow["colType"] = "面状要素";
                            }
                        }
                        //m_dataRow["colTime"] = pDataset.CreationTime.ToLocalTime().ToString();


                        #endregion
                    }
                    else if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                    {
                        #region 要素集
                        m_dataRow["colPic"] = this.imageList1.Images[3];
                        m_dataRow["colType"] = "要素集";
                        m_dataRow["colTime"] = "";
                        #endregion
                    }
                    else if (pDataset.Type == esriDatasetType.esriDTRasterDataset)
                    {
                        #region 栅格数据
                        m_dataRow["colPic"] = this.imageList1.Images[2];
                        m_dataRow["colType"] = "栅格数据";
                        m_dataRow["colTime"] = "";
                        #endregion
                    }
                    pDataset = pEnumDataset.Next();
                    #endregion
                }
                this.gridView1.GridControl.DataSource = m_dataTable;
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumDataset);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspace);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspaceFactory);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                #endregion
            }
            else if (m_dataRow["colType"].ToString() == "要素集")
            {
                #region 要素集
                int index1 = this.directoryComboBoxEdit1.Text.Trim().LastIndexOf("\\");
                string str1 = this.directoryComboBoxEdit1.Text.Trim().Remove(index1);
                int index2 = str1.LastIndexOf("\\");
                //mdb路径
                string str2 = str1.Remove(index2);

                IWorkspaceFactory pWorkspaceFactory;
                IWorkspace pWorkspace;
                pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                pWorkspace = pWorkspaceFactory.OpenFromFile(str2, 0);

                IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;

                IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(m_dataRow["colName"].ToString());

                IEnumDataset pEnumDataset = pFeatureDataset.Subsets;
                pEnumDataset.Reset();


                //新建一个dataTable
                DataTable pdataTable = new DataTable();

                pdataTable.Columns.Add("colPic", typeof(Image));
                pdataTable.Columns.Add("colName", typeof(string));
                pdataTable.Columns.Add("colType", typeof(string));
                pdataTable.Columns.Add("colTime", typeof(string));

                IDataset pDataset = pEnumDataset.Next();
                while (pDataset != null)
                {
                    if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                    {
                        #region 要素类
                        IFeatureClass pFeatureClass = pDataset as IFeatureClass;

                        if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                        {
                            m_dataRow = pdataTable.Rows.Add();
                            m_dataRow["colName"] = pFeatureClass.AliasName;
                            m_dataRow["colPic"] = this.imageList1.Images[8];
                            m_dataRow["colType"] = "图层注记";
                        }
                        else
                        {
                            if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                            {
                                m_dataRow = pdataTable.Rows.Add();
                                m_dataRow["colName"] = pFeatureClass.AliasName;
                                m_dataRow["colPic"] = this.imageList1.Images[5];
                                m_dataRow["colType"] = "点状要素";
                            }
                            else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                            {
                                m_dataRow = pdataTable.Rows.Add();
                                m_dataRow["colName"] = pFeatureClass.AliasName;
                                m_dataRow["colPic"] = this.imageList1.Images[6];
                                m_dataRow["colType"] = "线状要素";
                            }
                            else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                            {
                                m_dataRow = pdataTable.Rows.Add();
                                m_dataRow["colName"] = pFeatureClass.AliasName;
                                m_dataRow["colPic"] = this.imageList1.Images[7];
                                m_dataRow["colType"] = "面状要素";
                            }
                        }

                        this.gridView1.GridControl.DataSource = pdataTable;
                        #endregion
                    }
                    pDataset = pEnumDataset.Next();

                }
                #endregion

            }
        }

        private void ensureBtn_Click(object sender, EventArgs e)
        {
            int count;   //选择的要素数
            count = this.gridView1.SelectedRowsCount;
            if (count >= 0)
            {
                int[] dataRowHandles = this.gridView1.GetSelectedRows();
                DataRow dr;    //存放循环中当前的行
                for (int i = 0; i < count; i++)
                {
                    dr = this.gridView1.GetDataRow(dataRowHandles[i]);
                    if (dr["colType"].ToString() == "点状要素" ||
                        dr["colType"].ToString() == "线状要素" ||
                        dr["colType"].ToString() == "面状要素" ||
                        dr["colType"].ToString() == "图层注记")
                    {
                        #region  mdb和shapefile文件
                        IFeatureClass pFeatureClass; // 添加到mapControl中的要素

                        if (this.directoryComboBoxEdit1.Text.ToUpper().Contains("MDB"))
                        {
                            int index1 = this.directoryComboBoxEdit1.Text.LastIndexOf("\\");
                            string str1 = this.directoryComboBoxEdit1.Text.Remove(index1);
                            int index2 = str1.LastIndexOf(".");
                            string str2 = str1.Substring(index2 + 1);
                            if (str2.ToUpper() == "MDB")
                            {
                                IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                                IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(str1, 0) as IFeatureWorkspace;

                                string a = dr["colName"].ToString();

                                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(dr["colName"].ToString());

                                string b = pFeatureClass.AliasName;

                                IFeatureLayer pFeatureLayer;
                                if (dr["colType"].ToString() == "图层注记")
                                {
                                    pFeatureLayer = new FDOGraphicsLayerClass();
                                }
                                else
                                {
                                    pFeatureLayer = new FeatureLayerClass();

                                }
                                pFeatureLayer.FeatureClass = pFeatureClass;
                                pFeatureLayer.Name = pFeatureClass.AliasName;

                                if (m_mapControl != null)
                                {
                                    m_mapControl.AddLayer(pFeatureLayer);
                                    m_mapControl.Refresh();
                                }
                            }
                            else
                            {
                                int index3 = str1.LastIndexOf("\\");
                                string path = str1.Remove(index3);

                                IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                                IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(path, 0) as IFeatureWorkspace;

                                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(dr["colName"].ToString());
                                IFeatureLayer pFeatureLayer;
                                if (dr["colType"].ToString() == "图层注记")
                                {
                                    pFeatureLayer = new FDOGraphicsLayerClass();

                                }
                                else
                                {
                                    pFeatureLayer = new FeatureLayerClass();

                                }
                                pFeatureLayer.FeatureClass = pFeatureClass;
                                pFeatureLayer.Name = pFeatureClass.AliasName;

                                if (m_mapControl != null)
                                {
                                    m_mapControl.AddLayer(pFeatureLayer);
                                    m_mapControl.Refresh();
                                }
                            }

                        }
                        else
                        {
                            IWorkspaceFactory pWorkspaceFactory;
                            pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                            IFeatureWorkspace pFeatureWorkspace;

                            int index = this.directoryComboBoxEdit1.Text.LastIndexOf("\\");
                            string path = this.directoryComboBoxEdit1.Text.Remove(index);   //路径
                            pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(path, 0) as IFeatureWorkspace;
                            pFeatureClass = pFeatureWorkspace.OpenFeatureClass(dr["colName"].ToString());

                            //加载到mapControl中
                            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                            pFeatureLayer.FeatureClass = pFeatureClass;
                            pFeatureLayer.Name = pFeatureClass.AliasName;

                            if (m_mapControl != null)
                            {
                                m_mapControl.AddLayer(pFeatureLayer);
                                m_mapControl.Refresh();
                            }
                        }


                        #endregion
                    }
                    else if (dr["colType"].ToString() == "栅格数据")
                    {
                        #region 栅格数据 
                        if (m_mapControl == null)
                        {
                            PublicVariablesClass.AddRasterFilePath[i] = directoryComboBoxEdit1.Text + dr["colName"];

                        }
                        else
                        {
                            IWorkspaceFactory pWorkspaceFactory = new RasterWorkspaceFactoryClass();
                            IRasterWorkspace pRasterWorkspace = pWorkspaceFactory.OpenFromFile(this.directoryComboBoxEdit1.Text.ToString(), 0) as IRasterWorkspace;
                            IRasterDataset pRasterDataset = new RasterDatasetClass();
                            pRasterDataset = pRasterWorkspace.OpenRasterDataset(dr["colName"].ToString());

                            IRasterLayer pRasterLayer = new RasterLayerClass();
                            pRasterLayer.CreateFromDataset(pRasterDataset);
                            if (m_mapControl != null)
                            {
                                m_mapControl.Map.AddLayer(pRasterLayer as ILayer);
                                m_mapControl.Refresh();
                            }

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspaceFactory);
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterWorkspace);
                        }
                        #endregion
                    }
                }
            }
            this.Close();

        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddDataDialogFrm_Load(object sender, EventArgs e)
        {
            if (PublicVariablesClass.LastOpenFilePath != "")
            {
                DataTable m_dataTable = new DataTable();
                this.directoryComboBoxEdit1.Text = PublicVariablesClass.LastOpenFilePath;
                if (this.directoryComboBoxEdit1.Text.ToUpper().Contains("MDB"))
                {
                    int temp1 = this.directoryComboBoxEdit1.Text.Trim().LastIndexOf("\\");
                    string strTemp = this.directoryComboBoxEdit1.Text.Trim().Remove(temp1);
                    int temp2 = strTemp.LastIndexOf("\\");
                    string strName = strTemp.Substring(temp2 + 1);

                    if (strName.ToUpper().Contains("MDB"))
                    {
                        #region Mdb数据

                        m_dataTable.Columns.Add("colPic", typeof(Image));
                        m_dataTable.Columns.Add("colName", typeof(string));
                        m_dataTable.Columns.Add("colType", typeof(string));
                        m_dataTable.Columns.Add("colTime", typeof(string));

                        int index = this.directoryComboBoxEdit1.Text.LastIndexOf("\\");
                        DataRow m_dataRow = m_dataTable.Rows.Add();

                        IWorkspaceFactory pWorkspaceFactory;
                        IWorkspace pWorkspace;
                        pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                        pWorkspace = pWorkspaceFactory.OpenFromFile(this.directoryComboBoxEdit1.Text.Remove(index), 0);

                        IEnumDataset pEnumDataset;
                        pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny);
                        pEnumDataset.Reset();

                        IDataset pDataset;
                        pDataset = pEnumDataset.Next();
                        while (pDataset != null)
                        {
                            #region 遍历数据集
                            m_dataRow["colName"] = pDataset.Name;
                            if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                            {
                                #region 要素类
                                IFeatureClass pFeatureClass = pDataset as IFeatureClass;

                                if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                                {
                                    m_dataRow = m_dataTable.Rows.Add();
                                    m_dataRow["colPic"] = this.imageList1.Images[8];
                                    m_dataRow["colType"] = "图层注记";
                                }
                                else
                                {
                                    if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                                    {
                                        m_dataRow = m_dataTable.Rows.Add();
                                        m_dataRow["colPic"] = this.imageList1.Images[5];
                                        m_dataRow["colType"] = "点状要素";
                                    }
                                    else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                                    {
                                        m_dataRow = m_dataTable.Rows.Add();
                                        m_dataRow["colPic"] = this.imageList1.Images[6];
                                        m_dataRow["colType"] = "线状要素";
                                    }
                                    else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                                    {
                                        m_dataRow = m_dataTable.Rows.Add();
                                        m_dataRow["colPic"] = this.imageList1.Images[7];
                                        m_dataRow["colType"] = "面状要素";
                                    }
                                }
                                //m_dataRow["colTime"] = pDataset.CreationTime.ToLocalTime().ToString();


                                #endregion
                            }
                            else if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                            {
                                #region 要素集
                                m_dataRow["colPic"] = this.imageList1.Images[3];
                                m_dataRow["colType"] = "要素集";
                                m_dataRow["colTime"] = "";
                                #endregion
                            }
                            else if (pDataset.Type == esriDatasetType.esriDTRasterDataset)
                            {
                                #region 栅格数据
                                m_dataRow["colPic"] = this.imageList1.Images[2];
                                m_dataRow["colType"] = "栅格数据";
                                m_dataRow["colTime"] = "";
                                #endregion
                            }
                            pDataset = pEnumDataset.Next();
                            #endregion
                        }
                        this.gridView1.GridControl.DataSource = m_dataTable;
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumDataset);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspace);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspaceFactory);
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        #endregion

                    }
                    else
                    {
                        #region 要素集

                        //mdb路径
                        string strPath = strTemp.Remove(temp2);

                        IWorkspaceFactory pWorkspaceFactory;
                        IWorkspace pWorkspace;
                        pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                        pWorkspace = pWorkspaceFactory.OpenFromFile(strPath, 0);

                        IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;

                        IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(strName);

                        IEnumDataset pEnumDataset = pFeatureDataset.Subsets;
                        pEnumDataset.Reset();


                        //新建一个dataTable
                        DataTable pdataTable = new DataTable();

                        pdataTable.Columns.Add("colPic", typeof(Image));
                        pdataTable.Columns.Add("colName", typeof(string));
                        pdataTable.Columns.Add("colType", typeof(string));
                        pdataTable.Columns.Add("colTime", typeof(string));

                        IDataset pDataset = pEnumDataset.Next();

                        DataRow m_dataRow;
                        while (pDataset != null)
                        {
                            if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
                            {
                                #region 要素类
                                IFeatureClass pFeatureClass = pDataset as IFeatureClass;

                                if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                                {
                                    m_dataRow = pdataTable.Rows.Add();
                                    m_dataRow["colName"] = pFeatureClass.AliasName;
                                    m_dataRow["colPic"] = this.imageList1.Images[8];
                                    m_dataRow["colType"] = "图层注记";
                                }
                                else
                                {
                                    if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                                    {
                                        m_dataRow = pdataTable.Rows.Add();
                                        m_dataRow["colName"] = pFeatureClass.AliasName;
                                        m_dataRow["colPic"] = this.imageList1.Images[5];
                                        m_dataRow["colType"] = "点状要素";
                                    }
                                    else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                                    {
                                        m_dataRow = pdataTable.Rows.Add();
                                        m_dataRow["colName"] = pFeatureClass.AliasName;
                                        m_dataRow["colPic"] = this.imageList1.Images[6];
                                        m_dataRow["colType"] = "线状要素";
                                    }
                                    else if (pFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                                    {
                                        m_dataRow = pdataTable.Rows.Add();
                                        m_dataRow["colName"] = pFeatureClass.AliasName;
                                        m_dataRow["colPic"] = this.imageList1.Images[7];
                                        m_dataRow["colType"] = "面状要素";
                                    }
                                }

                                this.gridView1.GridControl.DataSource = pdataTable;
                                #endregion
                            }
                            pDataset = pEnumDataset.Next();

                        }
                        #endregion

                    }
                }
                else
                {
                    ChangeView();
                }
            }
        }

        private void AddDataDialogFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            PublicVariablesClass.LastOpenFilePath = this.directoryComboBoxEdit1.Text;
        }













        
        


    }
}